package revontuli.aMHD;

import java.util.ArrayList;

import revontuli.aMHD.UIhelpers.StopListAdapter;
import revontuli.aMHD.dataclasses.Stop;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

public class StopListAct extends Activity {
	private ListView mListView;
	private StopListAdapter mListAdapter;

	UserLocationSvc mService;
	private ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			UserLocationSvc.ClientBinder binder = (UserLocationSvc.ClientBinder) service;
			mService = binder.getService();
			mService.registerMessenger(null);
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.stoplist);
		
		mListView = (ListView) findViewById(R.id.StopListView);
		ArrayList<Stop> mStops = aMHDApp.mDataManager.getAllStops();
		mListAdapter = new StopListAdapter(this, mStops);
		mListView.setAdapter(mListAdapter);
		
		
	}
	
	@Override
	public void onStart() {
		super.onStart();
		bindService(new Intent(this, UserLocationSvc.class), mConnection, Context.BIND_AUTO_CREATE);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		if (mService != null) {
			unbindService(mConnection);
		}
	}
	
	//metody menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.stoplistmenu, menu);
		return true;
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if (aMHDApp.mDataManager.getOffline()) { //offline mod
			//5-offline button, 6-online button
			menu.getItem(5).setVisible(false);
			menu.getItem(6).setVisible(true);
			menu.getItem(1).setEnabled(false);
			menu.getItem(2).setEnabled(false);
		} else { //online mod
			menu.getItem(5).setVisible(true);
			menu.getItem(6).setVisible(false);
			menu.getItem(1).setEnabled(true);
			menu.getItem(2).setEnabled(true);
		}
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.MenuSLFinder:
			startActivity(new Intent(getApplicationContext(), StopFinderAct.class));
			return true;
		case R.id.MenuSLMap:
			startActivity(new Intent(getApplicationContext(), StopMapAct.class));
			return true;
		case R.id.MenuSLConnection:
			startActivity(new Intent(getApplicationContext(), ConnFinderAct.class));
			return true;
		case R.id.MenuSLLineList:
			startActivity(new Intent(getApplicationContext(), LineListAct.class));
			return true;
		case R.id.MenuSLSettings:
			startActivity(new Intent(getApplicationContext(), SettingsAct.class));
			return true;
		case R.id.MenuSLOffline:
			aMHDApp.mDataManager.setOffline(true);
			return true;
		case R.id.MenuSLOnline:
			aMHDApp.mDataManager.setOffline(false);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}


}
